;;; yisp-stock.el --- Duzy's goodies of buying and seling stocks.

;; Copyright (C) 2007  Duzy Chan

;; Author: Duzy Chan <duzy@duzy.info>
;; Keywords: convenience

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; 

;;; Code:

(require 'widget)

;;(eval-when-compile
;;  (require 'wid-edit))

(defvar widget-suggest-result nil)
(defvar widget-open nil)
(defvar widget-close nil)

(defun yisp-stock-editable-field-updated (widget &rest ignore)
  "Notifier callback of the editable-fields"
  (if widget-suggest-result
      (let ((tag (widget-get widget :tag))
            (v (widget-value widget)))
        (message tag)
        (if t ;;(or (eq tag "open") (eq tag "close"))
            (let ((open-price (float (string-to-number
                                      (widget-value widget-open))))
                  (close-price (float (string-to-number
                                       (widget-value widget-close))))
                  (change 0.0))
              (setq change (* 100 (/ (- close-price open-price)
                              open-price)))
              (if (< 10.0 change)
                  (setq v (format "change=%f%%, stock SHOULD be saled!" change))
                (setq v (format "change=%f%%" change)))))
        
        (widget-value-set widget-suggest-result v))
    (yisp-message "Invalid widget-suggest-result.")))

(defvar editable-field-callback
  (lambda (widget &rest ignore)
    (yisp-stock-editable-field-updated widget ignore)))

(defun yisp-stock-make-local-variables ()
  (make-local-variable 'widget-suggest-result)
  (make-local-variable 'widget-open)
  (make-local-variable 'widget-close)
  (make-local-variable 'editable-field-callback))

;;;###autoload
(defun yisp-stock ()
  "Helping to buy/sale stock."
  (interactive)
  (switch-to-buffer "*stock*")
  (kill-all-local-variables)
  (yisp-stock-make-local-variables)
  (let ((inhibit-read-only t))
    (erase-buffer))
  (remove-overlays)
  (widget-insert "Details of stock(code: 601872)\n")
  (widget-create 'editable-field
                 :size 10 :tag "date"
                 :format "\tDate:\t%v\n"
                 :notify editable-field-callback
                 "0.0")
  (setq widget-open (widget-create 'editable-field
                 :size 10 :tag "open"
                 :format "\tOpen:\t%v\n"
                 :notify editable-field-callback
                 "0.0"))
  (widget-create 'editable-field
                 :size 10 :tag "low"
                 :format "\tLow:\t%v\n"
                 :notify editable-field-callback
                 "0.0")
  (widget-create 'editable-field
                 :size 10 :tag "high"
                 :format "\tHigh:\t%v\n"
                 :notify editable-field-callback
                 "0.0")
  (setq widget-close (widget-create 'editable-field
                 :size 10 :tag "close"
                 :format "\tClose:\t%v\n"
                 :notify editable-field-callback
                 "0.0"))
  (widget-create 'editable-field
                 :size 10 :tag "volume"
                 :format "\tVolume:\t%v\n"
                 :notify editable-field-callback
                 "0.0")
  (setq widget-suggest-result
        (widget-create 'editable-field
                       :tag "suggest"
                       :format "    Suggest: %v\n"))
  (widget-value-set widget-suggest-result "N/A")
  (widget-setup))

(provide 'yisp-stock)
;;; yisp-stock.el ends here
